﻿function FilterHandler() {
	var _this = this;
	this.grid = null;
	this.shown = false;
	this.filterBar = null;
	this.selections = new Map();
	
	this.pageContentChangedListener = function() {
		_this.selections = new Map();
	};
	
	this.clearFilters = function() {
		this.selections = new Map();
		_this.grid.getDataRows().each(function(r, row) {
			$(row).show();
		});
		_this.grid.pageChanged.trigger();
		_this.grid.resize();
	};
	
	this.applyFilters = function() {
		_this.grid.getDataRows().each(function(r, row) {
			var isRowOk = true;
			$(_this.grid.getHeaderCells()).each(function(i, th) {
				var coldef = _this.grid.getColumnDefinition(th);
				var cellvalue = _this.grid.getNodeForRow(row).record[coldef.field];
				
				if (_this.selections.contains(coldef.field)) {
					isRowOk &= _this.selections.get(coldef.field).get(cellvalue);
				}
			});
			isRowOk ? $(row).show(): $(row).hide();
		});
		_this.grid.pageChanged.trigger();
		_this.grid.resize();
	};
	
	this.showConfiguration = function(th) {
		var coldef = _this.grid.getColumnDefinition(th);
		
		if (!_this.selections.contains(coldef.field)) {
			var rows = _this.grid.getDataRows();
			
			var vals = new Map();
			for (var i=0; i < rows.size();i++) {
				var node = _this.grid.getNodeForRow(rows.get(i));
				
				if (node != null) {
					vals.put(node.record[coldef.field], true);
				}
			}
			_this.selections.put(coldef.field, vals);
		}
		var vals = _this.selections.get(coldef.field);
		var list = $('<div>').height(150).css('background', 'white').css('overflow', 'auto');
		var regex = $('<div>').append(
			'Filter regular expression:',
			$('<input>').addClass('filter-regex').attr('type', 'text'),
			$('<button>').html('OK').button().click(function() {
				var regexp = new RegExp("^" +regex.find('.filter-regex').val());
				var items = list.find('input.filter-item'), i = items.size();
				while (i--) {
					items.get(i).checked = regexp.test(items.get(i).value);
					$(items.get(i)).change();
				}
			})
		);
		
		$(vals.getKeys().sort()).each(function(i, key) {
			var chk = $('<input>').addClass('filter-item').attr('checked', vals.get(key)).attr('type', 'checkbox').val(key).change(function() {
				vals.put(this.value, this.checked);
			});
			list.append($('<div>').append(chk).append('<span>'+key+'</span>'));
		});
		var dialog = $('<div>').attr('title', 'Filter [' + vals.size() + ' unique items found]').append(list, regex).dialog({
			resizable: false,
			width:600,
			modal: true,
			buttons: {
				"Select none": function() {$(this).find('input.filter-item').attr('checked', false).change();},
				"Select all": function() {$(this).find('input.filter-item').attr('checked', true).change();},
				"Cancel": function() {$(this).dialog( "close" );},
				"OK": function() {_this.applyFilters(); $(this).dialog( "close" );}
			}
		});
	};
	
	this.toggleFilter = function() {
		_this.shown = !_this.shown;
		
		if (_this.shown) {
			_this.grid.getHeaderCells().each(function(index) {
				$(this).find('.dt-table-value').append($('<div>').addClass('dt-filter-button ui-icon ui-icon-gear').css('display', 'inline-block').click(function(e) {
						_this.showConfiguration(getFirstAncestorOfType(this, 'TH'));
						e.cancelBubble = true;
						if (e.stopPropagation) e.stopPropagation();
					})
				);
			});
		} else {
			_this.grid.getHeaderCells().each(function(index) {
				$(this).find('.dt-filter-button').remove();
			});
			this.clearFilters();
		}
	};
	
	this.detach = function() {
		this.grid.pageContentChanged.remove(this.pageContentChangedListener);
		this.shown = true;
		this.toggleFilter();
		this.filterBar.remove();
	};
	
	this.attach = function(grid) {
		this.grid = grid;
		var id = "filterBarButton_" + new Date().getTime();
		this.filterBar = $('<span>').append(
			$('<input>').attr('type', 'checkbox').attr('id', id).click(function() {
				_this.toggleFilter();
				$(this).parent().find('.ui-button-text').html('Filter: ' + (_this.shown ? 'ON':'OFF'));
			}),
			$('<label>').attr('for', id).html('Filter')
		);
		this.filterBar.find('input').button({icons: {primary: "ui-icon-gear"}});
		
		
		if (_this.grid.addToolbarWidget) _this.grid.addToolbarWidget(this.filterBar);
		
		this.grid.pageContentChanged.add(this.pageContentChangedListener);
	}
}; FilterHandler.inherits(DatagridPlugin);